package com.szbber.door.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.szbber.door.R;



/**
 * @author ChayChan
 * @description: 对glide进行封装的工具类
 * @date 2017/6/19  20:43
 */

public class GlideUtils {

    public static void load(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions();
//        options.placeholder(R.drawable.ic_default);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }

    public static void load_e8_bg(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.color.translucent);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }


    public static void load(Context context, String url, ImageView iv, int placeHolderResId) {
        if (placeHolderResId == -1) {
            Glide.with(context)
                    .load(url)
                    .into(iv);
            return;
        }
        RequestOptions options = new RequestOptions();
        options.placeholder(placeHolderResId);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }

    public static void loadRound(Context context, String url, ImageView iv) {
//        RequestOptions options = new RequestOptions();
//        options.placeholder(R.drawable.ic_circle_default)
//                .centerCrop()
//                .circleCrop();
//
//        Glide.with(context)//
//                .load(url)//
//                .apply(options)//
//                .into(iv);
    }

    public static void LoadImgToBackground(Activity activity, String url, final View view) {
        if (activity == null) {
        } else {
            Glide.with(activity)
                    .asBitmap()
                    .load(url)
                    .into(new SimpleTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                            Drawable drawable = new BitmapDrawable(resource);
                            view.setBackground(drawable);
                        }
                    });
        }
    }

    public static void LoadImgToBackground(Context activity, String url, final View view) {
        Glide.with(activity)
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        Drawable drawable = new BitmapDrawable(resource);
                        view.setBackground(drawable);
                    }
                });
    }

    public static void loadLoact(Context context, int resourceId, ImageView imageView) {
        RequestOptions options = new RequestOptions();
        options.placeholder(R.color.translucent);
        Glide.with(context)
                .load(resourceId)
//                .apply(options)
                .into(imageView);
    }

}
